<?php

class FeedbackController extends Zend_Controller_Action {

    public function preDispatch() {
        if (!defined('PAGE_ID'))
            $this->_redirect(SITE_URL);
    }

    public function indexAction() {
        //Отображаем форму
        $sid = (int) $this->getRequest()->getParam('sid', 0);
        $model = new FeedbackModel();
        $content = $model->getContent($sid);
        $forms_form = new FormsForm();
        $forms_model = new FormsModel();

        $form = $forms_form->getForm($content->fid, SITE_URL . PAGE_URL . '#feedback');
        $this->view->form = $form;
        if ($this->_request->isPost() && $content->fid > 0) {
            $formData = $this->_request->getPost();
            if ($form->isValid($formData)) {
                array_pop($formData);
                $form_object = $forms_model->getFields($content->fid);
                $body = '';
                foreach ($form_object as $key => $v) {
                    if ($v->field <> 'captcha') {
                        if ($v->type == 'select') {
                            $list = $forms_model->getList($v->table, $v->table_field, $v->where, $v->order);
                            $body .= $v->title . ': ' . $list[$formData[$v->field]] . '<br>';
                        } else {
                            $body .= $v->title . ': ' . str_replace("\n", '<br>', $formData[$v->field]) . '<br>';
                        }
                    }
                }
                $mailModel = new MailModel();
                if ($mailModel->sendMail($content->email, $content->title, $body, true)) {
                    if ($content->redirect > 0) {
                        $redirect_page = $this->view->pages[$content->redirect];
                        if (isset($redirect_page->url)) 
                            $this->_redirect(SITE_URL . $redirect_page->url);
                    }
                    $this->view->success_send = true;
                }
            } else {
                $form->populate($formData);
            }
        }
    }

    public function modifyAction() {
        $model = new FeedbackModel();
        $sid = (int) $this->getRequest()->getParam('sid', 0);
        $forms = new FeedbackForm();
        $form = $forms->Form($sid);
        $this->view->form = $form;
        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
            if ($form->isValid($formData)) {
                array_pop($formData);
                $model->updateContent($formData, $sid);
                $this->_redirect('/pages/modify/id/' . $sid);
            } else {
                $form->populate($formData);
            }
        } else {
            $form->populate((array) $model->getContent($sid));
        }
    }

    public function addAction() {
        $model = new FeedbackModel();
        $sid = (int) $this->getRequest()->getParam('sid', 0);
        $pid = (int) $this->getRequest()->getParam('pid', 0);
        $model->addContent(array('sid' => $sid, 'fid' => 0));
        $this->_redirect('/pages/section/id/' . $pid);
    }

}
